<template>
    <div>
        <my-head title="用户中心"></my-head>
        <van-form @submit="onSubmit">
            <van-field
                v-model="username"
                name="用户名"
                label="用户名"
                placeholder="用户名"
            />
            <van-field
                v-model="password"
                type="password"
                name="密码"
                label="密码"
                placeholder="密码"
            />
            <div style="margin: 16px;">
                <van-button round block type="info" native-type="submit">
                提交
                </van-button>
            </div>
        </van-form>
    </div>
</template>

<script>
import myHead from '@/components/head'
import { Toast } from 'vant'
import {useLogin} from '../utils/api'
export default {
    name: '',
    components: {
        myHead
    },
    data() { 
        return {
            username: '',
            password: ''
        }
    },
    created() {

    },
    methods:{
        onSubmit() {
            if(!this.username) {
                Toast('请输入用户名')
            } else if(!this.password) {
                Toast('请输入密码')
            } else {
                var obj = {
                    mobile: this.username,
                    pwd: this.password
                }
                useLogin(obj).then(res => {
                    localStorage.setItem('token', res.token)
                    if(this.$route.query.id) {
                        this.$router.push({
                            path: '/detail',
                            query: {
                                id: this.$route.query.id
                            }
                        })
                    } else {
                        this.$router.back()
                    }
                })
            }
        } 
    }
 }
</script>

<style scoped>
</style>